<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="card">
      <el-button type="primary" @click="add" class="c1">添加角色</el-button>
      <el-table :data="tableData" stripe border="">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
              :class="[i1 === 0 ? 'ii' : '', 'io', 'juzhong', 'cc']"
            >
              <!-- //一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="guanbi(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                  :class="[i2 === 0 ? '' : 'ii', 'juzhong']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="guanbi(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="guanbi(scope.row, item3.id)"
                      >{{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="roleName" label="角色描述"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色名称"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="bianji(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-menu"
              @click="fenpei(scope.row)"
              >分配</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="分配权限"
      :visible.sync="XiandialogVisible"
      width="50%"
      @close="clock"
    >
      <el-tree
        :data="treelist"
        :props="treerole"
        default-expand-all
        show-checkbox
        node-key="id"
        :default-checked-keys="defKeys"
        ref="treeref"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="XiandialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRight">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加用户 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addListshow"
      width="50%"
      @close="qingk"
    >
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="ruleForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addListshow = false">取 消</el-button>
        <el-button type="primary" @click="addlist">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog
      title="编辑用户"
      :visible.sync="bianjiListshow"
      width="50%"
      @close="bianjiclose"
    >
      <el-form
        :model="bianjiForm"
        status-icon
        :rules="bianjirules"
        ref="bianjiFormref"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="bianjiForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="bianjiForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bianjiListshow = false">取 消</el-button>
        <el-button type="primary" @click="bianjilist">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getjueselist,
  getjueselistId,
  gettree,
  getkeyid,
  gettianjia,
  getbianji,
  getbianjishuju,
  getshanchu,
} from "@/api/list";
export default {
  name: "jiaoSe",
  data() {
    return {
      tableData: [],
      treelist: [],
      XiandialogVisible: false,
      treerole: {
        label: "authName",
        children: "children",
      },
      defKeys: [],
      roleid: "",
      // 添加
      addListshow: false,
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        miaoshu: [
          {
            required: true,
            message: "请输入描述",
            trigger: "blur",
          },
          {
            min: 1,
            max: 5,
            message: "长度在 1 到 5 个字符",
            trigger: "blur",
          },
        ],
      },
      //编辑
      bianjiListshow: false,
      bianjiForm: {
        roleName: "",
        roleDesc: "",
      },
      bianjirules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        miaoshu: [
          {
            required: true,
            message: "请输入描述",
            trigger: "blur",
          },
          {
            min: 1,
            max: 5,
            message: "长度在 1 到 5 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getjuese();
  },
  methods: {
    async getjuese() {
      const res = await getjueselist();
      console.log(res);
      this.tableData = res.data;
    },
    async guanbi(row, id1) {
      // console.log(id, id1);
      const chuanid = await getjueselistId(row.id, id1);
      // console.log(chuanid);
      const close = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (close !== "confirm") {
        return this.$message.info("取消删除");
      }
      this.$message.success("删除成功");
      row.children = chuanid.data;
    },
    async fenpei(role) {
      const tree = await gettree();
      this.roleid = role.id;
      this.treelist = tree.data;
      // console.log(this.treelist);
      this.getLeagKeys(role, this.defKeys);
      this.XiandialogVisible = true;
    },
    getLeagKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getLeagKeys(item, arr));
    },
    clock() {
      this.defKeys = [];
    },
    async allotRight() {
      const keys = [
        ...this.$refs.treeref.getCheckedKeys(),
        ...this.$refs.treeref.getHalfCheckedKeys(),
      ];
      // console.log(keys);
      const keyid = keys + ",";
      const oider = await getkeyid({ rids: keyid }, this.roleid);
      // console.log(oider);
      if (oider.meta.status !== 200) {
        return this.$message.error("更新失败");
      }
      (this.XiandialogVisible = false), this.$message.success("更新成功");
      this.getjuese();
    },
    add() {
      this.addListshow = true;
    },
    async addlist() {
      const shuju = this.ruleForm;
      console.log(shuju);
      this.$refs.ruleForm.validate(async (valid) => {
        const tianjia = await gettianjia(shuju);
        // console.log(valid);
        if (valid) {
          this.$message.success("添加成功");
          this.addListshow = false;
          this.getjuese();
          // console.log(res2);
        }
        if (tianjia.meta.status !== 201) {
          this.$message.error("添加失败");
        }
      });
    },
    qingk() {
      this.$refs.ruleForm.resetFields();
    },
    //编辑
    async bianji(id) {
      this.bianjiListshow = true;
      const xiu = await getbianjishuju(id);
      console.log(xiu);
      // console.log(xiu);
      this.bianjiForm = xiu.data;
    },
    bianjiclose() {
      this.$refs.bianjiFormref.resetFields();
    },
    bianjilist() {
      this.$refs.bianjiFormref.validate(async (valid) => {
        // console.log(valid);
        const rea = await getbianji(this.bianjiForm);
        // console.log(rea);
        if (valid) {
          this.$message.success("修改成功");
          this.bianjiListshow = false;
          this.getjuese();
        }
        if (rea.meta.status !== 200) {
          this.$message.error("修改失败");
        }
      });
    },
    //删除
    async del(id) {
      const quxiao = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {
        return err;
      });
      if (quxiao !== "confirm") {
        return this.$message.info("取消删除");
      }
      // console.log(quxiao);
      const ii = await getshanchu(id);
      console.log(ii);
      this.$message.success("删除成功");
      this.getjuese();
    },
  },
};
</script>

<style lang="scss" scoped>
.card {
  margin-top: 15px;
}
.el-tag {
  margin: 7px;
}
.ii {
  border-top: 1px solid #eee;
}
.io {
  border-bottom: 1px solid #eee;
}
.juzhong {
  display: flex;
  align-items: center;
}
.cc {
  padding-left: 20px;
}
.c1 {
  margin-bottom: 20px;
}
</style>
